// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#ifndef CANGJIE_MIGRATOR_UTIL_IO
#define CANGJIE_MIGRATOR_UTIL_IO

#include <vector>
#include <string>
#include <iostream>
#include <fstream>

#include "Path.h"

namespace CangjieMigrator::Util::Io {

inline std::vector<std::string> ReadAllLines(const Path &file)
{
    std::ifstream f(file);
    if (!f) {
        std::cerr << "Open read " << file << " failed" << std::endl;
        return {};
    }

    std::vector<std::string> result{};
    std::string line;
    while (std::getline(f, line)) {
        result.push_back(line);
    }
    return result;
}

inline bool WriteAllLines(const Path &target, const std::vector<std::string> &lines)
{
    std::ofstream f(target);
    if (!f) {
        return false;
    }

    for (const auto &line : lines) {
        f << line << std::endl;
    }
    return true;
}

} // namespace CangjieMigrator::Util::Io

#endif // CANGJIE_MIGRATOR_UTIL_IO
